/*
 * UnitImpl.cc
 *
 *  Created on: Sep 12, 2011
 *      Author: topcoder
 */
#include "LengthUnit.h"
#include <sstream>
using namespace std;

LengthUnit::LengthUnit(int factor)
:factor(factor)
{
}

int LengthUnit::getFactor() const
{
	return factor;
}

string LengthUnit::formatToString(int amount)
{
	int mile = 0, yard = 0, feet = 0;
	mile = amount/(1760*3);
	yard = (amount - 1760*3*mile)/3;
	feet = amount - 1760*3*mile -3* yard;

	stringstream out;

	if(0 != mile)
		out<<mile<<" Mile";
	if(0 != yard)
	{
		if(0 != mile)
		{
			out<<" and ";
		}
		out<<yard<<" Yard";
	}
	if(0 != feet)
	{
		if(0 != mile || 0 != yard)
		{
			out<<" and ";
		}
		out<<feet<<" Feet";
	}

	if(!(feet||mile||yard))
	{
		out<<"0 Feet";
	}

	return out.str();

}

string LengthUnit::formatToStringInBaseUnit(int amount)
{
    stringstream out;
    out<<amount<<" Feet";
    return out.str();
}

LengthUnit& LengthUnit::MILE()
{
	static LengthUnit mile(1760*3);
	return mile;
}

LengthUnit& LengthUnit::YARD()
{
	static LengthUnit yard(3);
	return yard;
}

LengthUnit& LengthUnit::FEET()
{
	static LengthUnit feet(1);
	return feet;
} 
